//
//  SYStatusRetweetView.swift
//  YANAWeibo
//
//  Created by mac on 16/5/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
/// 转发微博
class SYStatusRetweetView: UIView {
    // 记录底部约束
    var retweetViewBottom: Constraint?
    
    //获得数据
    var statusViewModel: SYStatusViewModel? {
        didSet {
//            contentLabel.text = statusViewModel?.retweenContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
            // 卸载约束
            retweetViewBottom?.uninstall()
            // 判断转发微博
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
            } else {
                //没有转发图片
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            
            }
        }
    }
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: HomeFontSize)
        
        // 多行显示
        label.numberOfLines = 0
        label.text = "haha"
        return label
    }()
    
    /// 显示图片视图
    private lazy var pictureView: SYStatusPictureView = {
        let view = SYStatusPictureView()
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        self.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        addSubview(contentLabel)
        addSubview(pictureView)
        pictureView.backgroundColor = self.backgroundColor
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
            //暂时写死
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        //当前视图的约束
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    }

}
